package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product/")
public class SkuManageController {

    @Autowired
    private ManageService manageService;

    //  /admin/product/saveSkuInfo
    //  接收数据.
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        //  调用服务层方法：
        this.manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //  /admin/product/list/{page}/{limit}
    @GetMapping("list/{page}/{limit}")
    public Result getSkuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SkuInfo skuInfo){
        //  设置一个分页对象：
        Page<SkuInfo> skuInfoPage = new Page<>(page,limit);
        //  调用服务层方法
        IPage<SkuInfo> iPage = this.manageService.getSkuInfoList(skuInfoPage,skuInfo);
        return Result.ok(iPage);
    }
    //  /admin/product/onSale/{skuId}
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        //  调用服务层方法：
        this.manageService.onSale(skuId);
        return Result.ok();
    }

    //  /admin/product/cancelSale/{skuId}
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        //  调用服务层方法：
        this.manageService.cancelSale(skuId);
        return Result.ok();
    }

    @GetMapping("getSkuInfo/{skuId}")
    public Result getSkuInfo(@PathVariable Long skuId) {
        return Result.ok(manageService.getFullSkuInfo(skuId));
    }

    @PostMapping("updateSkuInfo")
    public Result updateSkuInfo(@RequestBody SkuInfo skuInfo) {
        // 调用服务层
        manageService.updateSkuInfo(skuInfo);
        return Result.ok();
    }

}
